home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: pInt = new int [987654321L], what will ha
- Message-ID: <marnoldDMA3n0.GqG@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <Pine.SOL.3.91.960203215559.25805E-100000@hamlet.uncg.edu> <31146e4c.197257088@nntp.ix.netcom.com> <Pine.SOL.3.91.960204100529.12786A-100000@hamlet.uncg.edu>
- Date: Mon, 5 Feb 1996 01:18:36 GMT
- Sender: marnold@netcom13.netcom.com
-
- "QIAN . ZHONG" <q_zhong@hamlet.uncg.edu> writes:
-
- >
-
- >On Sun, 4 Feb 1996, Mike Rubenstein wrote:
-
- >> "QIAN . ZHONG" <q_zhong@hamlet.uncg.edu> wrote:
- >>
- >> >
- >> > Hi, folks:
- >> >
- >> > Here is a question about new, the following is my code:
- >> >
- >> > int main(void)
- >> > {
- >> > int *pInt;
- >> > long Block = 987654321L;
- >> >
- >> > pInt = new int[Block];
- >> > if(pInt == NULL) dosomething();
- >> > }
- >> >
- >> > My problem is when I compile the program for DOS under large memory
- >> > model, above new nerve return NULL, I guess compiler convert
- >> >
- >> > new int[987654321L] --- convert --> new int[ ACONST]
- >> > where ACONST = 0xffff , or ACONST = 0x7fff, am I right ?
- >> > Is this a DOS thing ? or a C++ thing ? Is this portable ?
- >>
- >> More likely the system just doesn't have 987 meg free memory, so it
- >> can't do the allocation. Newer compilers will throw an exception, but
- >> older ones simply return the null pointer.
- >>
- >Hi, Michael:
-
- >Thanks for your message, my real problem is the compiler will not return a
- >null pointer, even if I set_new_handler(0);
-
- >I am confused. Please help me again. I use BC++ 4.0.
-
- Your problem is related to compiling for DOS. Most compilers use make
- size_t equivalent to a int. DOS comilers usually make ints 16-bit
- quantities and longs 32-bit quantities.
-
- Since new is prototyped as follows...
-
- void* operator new(size_t size);
-
- ...it follows that you can only allocate chunks of memory whose size can
- be expressed with 16-bits using a DOS C++ compiler.
-
- So, when you write...
-
- new int[BIG_NUMBER];
-
- ...where BIG_NUMBER is a value which cannot be expressed in 16-bits and
- passed to new as a size_t (a 16-bit int, remember?), the compiler will
- perform an implicit conversion for you (often with an accompanying
- warning). In fact, compiling your sample code above, Borland C++ 4.52
- issues the following warning...
-
- Warning CRAP.CPP 10: Conversion may lose significant digits in function main()
-
- This warning means that the 32-bits required to represent 987654321 are
- being truncated to 16. New will be passed 26801 (that's 897654321 with it's
- top 16 bits missing). This is a perfectly reasonable value for the
- tyipcally 16-bit memory management routines included with DOS compilers
- to allocate. Thus, your code above will probably always succeed. You're
- only asking new for 27K or so.
-
- If you wish to allocate blocks of memory larger than 64K (16-bits allows you
- to express numbers up to 65535, or 64K, right?), in a DOS environment, most
- DOS compilers will let you use "huge" pointers, along with accomanying "huge"
- memory allocations, in which 32-bit numbers can be used.
-
- For example, your sample program can be modified to do what you really
- wanted it to do under borland C++ 4.52...
-
- int main(void)
- {
- int huge* pInt;
- long Block = 8000000L; // see below
-
- pInt = new huge int[Block];
- if(pInt == 0)
- dosomething();
-
- return 0;
- }
-
- I changed the block size to about 8 megabytes instead of over 987 megabytes,
- which was a bit unreasonable. Now, this program works properly. However,
- it now uses the non-standard keyword "huge" (common to most DOS compilers,
- tho).
-
- And, it was working properly before, except you didn't understand what was
- happening to your 32-bit 987654321 block size when passed to a 16-bit new.
-
- Had you heeded the compiler warning (I do hope you have all warnings
- enabled), you probably could have tracked down this problem yourself
- instead of posting it to the world.
-
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-